/*
 * Copyright (C) 2015 备胎金服
 * 未经授权允许不得进行拷贝和修改
 *   http://www.btjf.com/
 */
package com.zhangman.androidgame;

import android.graphics.Rect;

/**
 * Created by zhangman on 2016/11/1 16:59.
 * Email: zhangman523@126.com
 */
public class Projectile {
  private int x, y, speedX;
  private boolean visible;

  private Rect r;

  public Projectile(int startX, int startY) {
    x = startX;
    y = startY;
    speedX = 7;
    visible = true;
    r = new Rect(0, 0, 0, 0);
  }

  public void update() {
    x += speedX;
    r.set(x, y, x + 10, y + 5);
    if (x > 800) {
      visible = false;
      r = null;
    }
    if (x < 800) {
      checkCollision();
    }
  }

  private void checkCollision() {
    if (Rect.intersects(r, GameScreen.hb.r)) {
      visible = false;
      if (GameScreen.hb.health > 0) {
        GameScreen.hb.health -= 1;
      }
      if (GameScreen.hb.health == 0) {
        GameScreen.hb.setCenterX(-100);
      }
    }
    if (Rect.intersects(r, GameScreen.hb2.r)) {
      visible = false;
      if (GameScreen.hb2.health > 0) {
        GameScreen.hb2.health -= 1;
      }
      if (GameScreen.hb2.health == 0) {
        GameScreen.hb2.setCenterX(-100);
      }
    }
  }

  public int getSpeedX() {
    return speedX;
  }

  public void setSpeedX(int speedX) {
    this.speedX = speedX;
  }

  public boolean isVisible() {
    return visible;
  }

  public void setVisible(boolean visible) {
    this.visible = visible;
  }

  public int getX() {
    return x;
  }

  public void setX(int x) {
    this.x = x;
  }

  public int getY() {
    return y;
  }

  public void setY(int y) {
    this.y = y;
  }
}
